package com.kcht.rfid.rd30.secretary.dispatch.impl;

import com.kcht.rfid.rd30.secretary.core.common.ReaderResponseStatus;
import com.kcht.rfid.rd30.secretary.core.response.DeviceStatusResponse;
import com.kcht.rfid.rd30.secretary.dispatch.BaseAbnormalDetection;
import com.kcht.rfid.rd30.secretary.dispatch.IReaderChannel;
import lombok.extern.slf4j.Slf4j;
import uhf_msg.Uhf;

import java.time.LocalDateTime;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

/**
 *驻波比检测
 * */
@Slf4j
public class SwrDetection extends BaseAbnormalDetection {
    private Timer timer;
    /**重复检测驻波比周期*/
    private long period=10000L;
    public SwrDetection(Observer observers) {
        super(observers);
    }

    @Override
    protected void detection(IReaderChannel channel) {
        this.timer=new Timer();
        this.timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("开始检测驻波比--->ip:{}",channel.getIp());
                int[] swrArray = calcSwrCheck(channel);
                if (swrArray != null) {
                    DeviceStatusResponse status=new DeviceStatusResponse();
                    status.setDateTime(LocalDateTime.now());
                    status.setIp(channel.getIp());
                    status.setStatus(ReaderResponseStatus.正常);
                    response(status);
                    for (int num=0; num < 4; num++) {
                        if (swrArray[num] > 200 && swrArray[num] != 10000) {
                            log.error("驻波比异常--->ip:{},天线1驻波比:{},天线2驻波比:{},天线3驻波比:{},天线4驻波比:{}",channel.getIp(),swrArray[0],swrArray[1],swrArray[2],swrArray[3]);
                            status.setStatus(ReaderResponseStatus.驻波比过大);
                            status.setMsg("天线1驻波比="+swrArray[0]+",天线2驻波比="+swrArray[1]+",天线3驻波比="+swrArray[2]+",天线4驻波比="+swrArray[3]);
                            response(status);
                            return;
                        }
                    }
                    log.info("驻波比正常--->ip:{},天线1驻波比:{},天线2驻波比:{},天线3驻波比:{},天线4驻波比:{}",channel.getIp(),swrArray[0],swrArray[1],swrArray[2],swrArray[3]);
                }
            }
        }, 0,period);
    }

    @Override
    protected void closeDetection() {
        this.timer.cancel();
        this.timer.purge();
    }


    // 发送驻波比request，上报断链状态
    private int[] calcSwrCheck (IReaderChannel channel) {
        Uhf.uhf_request request =calcSwrRequest();
        try{
            byte[] recv = channel.sendRecv(request.toByteArray());
            Uhf.uhf_response.CalcSwrResponse swrRes = Uhf.uhf_response.parseFrom(recv).getSwrResponse();
            return new int[]{swrRes.getSwr1(),swrRes.getSwr2(),swrRes.getSwr3(),swrRes.getSwr4()};
        }catch (Exception e) {
            log.error("检测到网络异常--->ip:{}",channel.getIp());
            DeviceStatusResponse status=new DeviceStatusResponse();
            status.setDateTime(LocalDateTime.now());
            status.setIp(channel.getIp());
            status.setStatus(ReaderResponseStatus.网络异常);
            this.response(status);
        }
        return null;
    }

    private  Uhf.uhf_request calcSwrRequest () {
        Uhf.uhf_request request = Uhf.uhf_request.getDefaultInstance();
        request = request.toBuilder()
                .setType(uhf_msg.Uhf.uhf_request.HeadType.calcSwr)
                .build();
        return request;
    }

    private void response(Object arg){
        this.setChanged();
        this.notifyObservers(arg);
    }
}
